Source Code Viewer - Struts Commons 1.7.3 - BigDecimalConverter

package name.matthewgreet.strutscommons.policy;

import java.math.BigDecimal;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import name.matthewgreet.strutscommons.annotation.BigDecimalConversion;
import name.matthewgreet.strutscommons.annotation.Required.MessageType;

public class BigDecimalConverter extends AbstractDefaultConverterSupport<BigDecimalConversion,BigDecimal> {
    private static final Logger LOG = LogManager.getLogger(BigDecimalConverter.class);
    
    @BigDecimalConversion
    private static boolean annotationPlaceholder;

	@Override
	protected BigDecimalConversion makeDefaultAnnotation() {
		try {
			return BigDecimalConverter.class.getDeclaredField("annotationPlaceholder").getAnnotation(BigDecimalConversion.class);
		}
    	catch (Exception e) {
			LOG.error("Creation of default annotation failed", e);
			return null;
		}
	}

    @Override
    public String format(BigDecimal unformattedValue) throws Exception {
        return unformattedValue.toString();
    }

    @Override
    public Class<BigDecimal> getRecipientClass() {
        return BigDecimal.class;
    }

    @Override
    public MessageType getMessageType() {
        return getAnnotation().messageType();
    }

    @Override
    public String getMessage() {
        return getAnnotation().message();
    }

    @Override
    public String getMessageKey() {
        return getAnnotation().messageKey();
    }

    @Override
    public String getRecipientFieldName() {
        return getAnnotation().parsedFieldName();
    }

    @Override
    public ConversionResult<BigDecimal> convert(String formValue, Class<? extends BigDecimal> recipientClass) throws Exception {
    	BigDecimal parsedValue;
        
        try {
            parsedValue = new BigDecimal(formValue);
            return ConversionResult.makeSuccessResult(parsedValue);
        }
        catch (NumberFormatException e) {
            return ConversionResult.makeFailureResult();
        }
    }

	@Override
	public boolean getProcessNoValue() {
        return false;
	}

}